/**
 * 
 */
package com.woniuxy.handler;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.woniuxy.pojo.ShopCart;
import com.woniuxy.service.CartService;
import com.woniuxy.util.DateUtil;

import lombok.Data;

@Controller
@RequestMapping("/cart")
@Data
public class ShopCartHandler {
	
	@RequestMapping("/cart")
	@ResponseBody
	public String update(HttpServletRequest request,int id,String img,String type,int rtid,BigDecimal price,int number,String date) throws Exception{
		//获取传过来预定时间
		DateUtil dateUtil=new DateUtil();
		ArrayList<java.sql.Date> lt=dateUtil.getCheckTimes(date);
		System.out.println(lt);
		//计算天数
		long day1=0;
		try {
			Date d1 = lt.get(0);// 转化第一个字符串为日期格式
			Date d2 =lt.get(1);// 转化第二个字符串为日期格式
			long days=(d2.getTime()-d1.getTime())/24/60/60/1000;
			day1=days+1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("天数-------"+day1);
		
		ShopCart sc=new ShopCart(id,img,type,rtid,price,number,lt.get(0),lt.get(1),day1);
		System.out.println("------------"+sc);
		String result="操作失败";
		HttpSession session=request.getSession();
		//获取购物车
		Object obj = session.getAttribute("myCart");
		Map<String,ShopCart> myCart=null;
		if(obj==null){
			myCart=new HashMap<String,ShopCart>();
		}else{
			myCart=(Map<String, ShopCart>) obj;
		}
		//判断是修改还是新增
		ShopCart currentDto = myCart.get(sc.getId()+"");
		if(currentDto!=null){
		//修改
		int numbers=currentDto.getNumber()+sc.getNumber();
		if(numbers>1){
			currentDto.setNumber(numbers);
			myCart.put(sc.getId()+"", currentDto);
			result="修改成功";
		}else{
			result="修改失败，数量至少为1";
		}
		}else{
		//新增
			myCart.put(sc.getId()+"", sc);
			result="添加成功";
		}
		session.setAttribute("myCart", myCart);
		return result;
	}
	
	@RequestMapping("/showMyCart")
	@ResponseBody
	public Collection<ShopCart> showMyCart(HttpServletRequest request){
		Collection<ShopCart> myCartVal=null;
		//获取购物车
		HttpSession session=request.getSession();
		Object obj = session.getAttribute("myCart");
		if(obj==null){
			session.setAttribute("myCart", new HashMap<String,ShopCart>());
		}else{
			myCartVal=((Map<String, ShopCart>)obj).values();
		}
		return myCartVal;
	}
	
	@RequestMapping("/deletCertGood")
	@ResponseBody
	public String deletCertGood(HttpServletRequest request,int id){
		String result="删除成功";
		HttpSession session=request.getSession();
		//获取购物车
		Object obj = session.getAttribute("myCart");
		Map<String,ShopCart> myCart=null;
		myCart=(Map<String, ShopCart>) obj;
		String key=id+"";
		myCart.remove(key);
		return result;
	}
	
	@RequestMapping("/addCertGood")
	@ResponseBody
	public String addCertGood(HttpServletRequest request,int id){
		HttpSession session=request.getSession();
		//获取购物车
		Object obj = session.getAttribute("myCart");
		Map<String,ShopCart> myCart=null;
		myCart=(Map<String, ShopCart>) obj;
		String key=id+"";
		ShopCart newDto = myCart.get(key);
		newDto.setNumber(newDto.getNumber()+1);
		myCart.put(key, newDto);
		String result="添加成功";
		return result;
	}
	
	@RequestMapping("/subCertGood")
	@ResponseBody
	public String subCertGood(HttpServletRequest request,int id){
		String result=null;
		HttpSession session=request.getSession();
		//获取购物车
		Object obj = session.getAttribute("myCart");
		Map<String,ShopCart> myCart=null;
		myCart=(Map<String, ShopCart>) obj;
		String key=id+"";
		ShopCart newDto = myCart.get(key);
		if(newDto.getNumber()>1){
			newDto.setNumber(newDto.getNumber()-1);
			myCart.put(key, newDto);
			result="减少成功";	
		}
		return result;
}
	@RequestMapping("/cleanCertGood")
	@ResponseBody
	public String cleanCertGood(HttpServletRequest request){
		String result=null;
		HttpSession session=request.getSession();
		//获取购物车
		Object obj = session.getAttribute("myCart");
		Map<String,ShopCart> myCart=null;
		myCart=(Map<String, ShopCart>) obj;
		myCart.clear();
		return result;
}
}
